//
//  SitesResultsViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-5-5.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SitesResultsViewController.h"
#import "BrandBottomViewController.h"
#import "SiteMapViewController.h"

@interface SitesResultsViewController ()

@end

@implementation SitesResultsViewController

@synthesize theTableView, coordinate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Results";
        
        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"Back";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        
        titles = [NSArray arrayWithObjects: @"CPMOORABBIN", @"The Good Guys - Moorabbin", @"RETRAVISION - Clayton",@"TIN DAT BETTA ELECTRICAL", @"COLDFLOW AIRCON PTY LTD", @"Newtone Electronics Springvale", @"HARVEY NORMAN MENTONE",@"Harvey Norman - Moorabbin", @"E & S TRADING PTY LTD", nil];
        
        detailTitles = [NSArray arrayWithObjects: @"MOORABBIN", @"Mentone",@"Clayton", @"SPRINGVALE", @"Springvale", @"Springvale",@"Springvale", @"Mentone", @"Mentone", nil];
        
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = 37.786996;
        theCoordinate.longitude = -122.419281;
        self.coordinate = theCoordinate;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIViewController *viewController = [[BrandBottomViewController alloc] initWithNibName:@"BrandBottomView" bundle:nil];
    //viewController.view.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:viewController.view];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table View datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:12];        
        cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
        
        cell.textLabel.backgroundColor= [UIColor clearColor];
        cell.detailTextLabel.backgroundColor= [UIColor clearColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-row-basics.png"]];
        UIImageView *highlightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-row-basicsb.png"]];
        cell.backgroundView = imageView;
        cell.selectedBackgroundView = highlightImageView;
	}
    
    cell.textLabel.text = [titles objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [detailTitles objectAtIndex:indexPath.row];
        
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [self goToMapView:cell.textLabel.text];
}

- (void) goToMapView:(NSString*)siteName
{
    if(siteMapViewController == nil){
        siteMapViewController = [[SiteMapViewController alloc]
                                      initWithNibName:@"SiteMapViewController" bundle:nil];
    }
    
    siteMapViewController.coordinate = self.coordinate;
    siteMapViewController.siteName = siteName;
    
    [[self navigationController] pushViewController:siteMapViewController animated:YES];
}

@end
